package com.ilxqx.generator.util;

import com.ilxqx.generator.config.ConfigProperties;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import java.util.List;
import java.util.Map;

/**
 * 数据库连接工具类
 * @author weixiaopeng
 */
public final class JdbcUtils {

    private static final JdbcTemplate JDBC_TEMPLATE;

    static {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(ConfigProperties.INSTANCE.getUrl());
        dataSource.setUsername(ConfigProperties.INSTANCE.getUsername());
        dataSource.setPassword(ConfigProperties.INSTANCE.getPassword());
        JDBC_TEMPLATE = new JdbcTemplate(dataSource);
    }

    /**
     * 执行查询sql
     * @param sql SQL语句
     * @param args 绑定参数
     * @return 执行结果
     */
    public static List<Map<String, Object>> query(String sql, Object... args) {
        return JDBC_TEMPLATE.queryForList(sql, args);
    }

}
